<script setup lang="ts">
import { User, Lock } from '@element-plus/icons-vue'
import { useUserstore } from '@/stores/userStore.ts'
import { useRouter } from 'vue-router'

const router = useRouter()
const useuser = useUserstore()
import { ref } from 'vue'
//@ts-ignore
import { ElNotification } from 'element-plus'

const userInfo = ref({
  user: 'admin',
  password: '111111',
})
const rules = ref({
  user: [
    { required: true, message: '用户名不能为空', trigger: 'blur' },
    { min: 5, message: '用户名长度不得小于5位', trigger: 'blur' },
  ],
  password: [{ required: true, message: '密码不能为空', trigger: 'blur' }],
})
const gettime = () => {
  const date = new Date()
  let time = ''
  const hour = date.getHours()
  if (hour >= 11 && hour <= 2) {
    time = '中午好'
  } else if (hour >= 5 && hour < 12) {
    time = '早上好'
  } else if (hour > 2 && hour <= 6) {
    time = '下午好'
  } else {
    time = '晚上好'
  }
  return time
}
const userref = ref()
const login = async () => {
  userref.value.validate(async (vaild: any) => {
    if (vaild) {
      try {
        await useuser.setuserInfo(userInfo.value.user, userInfo.value.password)
        let title = gettime()
        ElNotification({
          type: 'success',
          title,
          message: '欢迎回来',
        })
        router.push('/')
      } catch (e) {
        ElNotification({
          type: 'error',
          message: (e as Error).message,
        })
      }
    } else {
      ElNotification.warning('请将账号密码填写完整')
    }
  })
}
</script>

<template>
  <div class="login_container">
    <!--一行24个span，左右两个col各占12个
       :xs 表示屏幕宽度小于768时候所占大小-->
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <el-form
          ref="userref"
          class="login_form"
          :rules="rules"
          :model="userInfo"
        >
          <h1>Hello</h1>
          <h2>欢迎来到硅谷甄选</h2>
          <el-form-item prop="user">
            <el-input v-model="userInfo.user" :prefix-icon="User"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              show-password
              type="password"
              v-model="userInfo.password"
              :prefix-icon="Lock"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              size="default"
              class="login_btn"
              @click="login"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<style scoped lang="scss">
.login_container {
  width: 100%;
  height: 100vh;
  background-image: url('@/assets/images/background.jpg');
  background-repeat: no-repeat;
  background-size: cover;

  .login_form {
    width: 60%;
    top: 40vh;
    position: relative;
    background: url('@/assets/images/login_form.png') no-repeat;
    background-size: cover;
    padding: 40px;

    h1 {
      color: white;
      font-size: 40px;
    }

    h2 {
      color: white;
      font-size: 20px;
      margin: 20px 0px;
    }

    .login_btn {
      width: 100%;
    }
  }
}
</style>
